package com.longyiqun.cm.controller.server;

import com.alibaba.fastjson.JSON;
import com.longyiqun.cm.PageUtil;
import com.longyiqun.cm.TablePage;
import com.longyiqun.cm.domain.BaseServerEntity;
import com.longyiqun.cm.domain.BuildRecordEntity;
import com.longyiqun.cm.domain.LoginInfo;
import com.longyiqun.cm.domain.TestCaseEntity;
import com.longyiqun.cm.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.io.IOException;

@RestController
@RequestMapping("/server")
public class ServerRestControllerImpl {
    @Autowired
    ServerService serverService;

    @Autowired
    PageUtil pageUtil;

    @RequestMapping("/getBaseServerForPaging")
    public String  getServerForPaging(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam(required = false) String BaseServerType, @RequestParam(required = false) String SystemType) {
        TablePage tp = pageUtil.getDataForPaging(serverService.getBaseServerForPaging(pageNumber, pageSize, BaseServerType, SystemType));
        return JSON.toJSONString(tp);
    }
    @RequestMapping("/getbuildRecordForPaging")
    public String  getbuildRecordForPaging(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize, @RequestParam(required = false) String vServerName, @RequestParam(required = false) String eAudit, @RequestParam(required = false) String vLeader) {
        TablePage tp = pageUtil.getDataForPaging(serverService.getbuildRecordForPaging(pageNumber, pageSize, vServerName, eAudit,vLeader));
        return JSON.toJSONString(tp);
    }
    @RequestMapping(value = "/buildAudit")
    public String  buildAudit( @RequestParam(required = false) String buildId) throws IOException {
            int  result = serverService.Auditbuild(buildId);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }
    @RequestMapping(value = "/")
    public String deployLogForPaging(@RequestParam(required = true) int pageNumber, @RequestParam(required = true) int pageSize,@RequestParam(required = false)String vServerName){
        TablePage tp = pageUtil.getDataForPaging(serverService.LookDeployLog(pageNumber,pageSize,vServerName));
        return JSON.toJSONString(tp);

    }

    @RequestMapping(value = "/updateBaseServer", method = RequestMethod.POST)
    public String updateBaseServer(@RequestBody BaseServerEntity baseServerEntity) {
        int result = serverService.updateServerService(baseServerEntity);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping(value = "/addBuildForm")
    public String addBuildForm(@RequestBody BuildRecordEntity buildRecordEntity) {
        int result = serverService.addBuildService(buildRecordEntity);
        if(result>0) {
            return JSON.toJSONString ("true");
        }else{
            return JSON.toJSONString ("false");
        }
    }

    @RequestMapping(value = "/userLogin")
    public String userLogin(@RequestBody LoginInfo loginInfo) {
        System.out.println(loginInfo.getUsername());
        LoginInfo result = serverService.getUserInfoSC(loginInfo);
        if (result.getPassword().equals(loginInfo.getPassword())){
            return JSON.toJSONString(result.getRole());
        }else {
            return JSON.toJSONString("fail");
        }
    }

    @RequestMapping("/baseServerSelect")
    public String  baseServerSelect(@RequestParam int pageNumber, @RequestParam int pageSize) {
        TablePage tp= pageUtil.getDataForPaging(serverService.getServerTypeForPaging(pageNumber, pageSize));
        return JSON.toJSONString (tp);
    }
    @RequestMapping("/productSelect")
    public String  productSelect(@RequestParam int pageNumber, @RequestParam int pageSize) {
        TablePage tp= pageUtil.getDataForPaging(serverService.getProductType(pageNumber, pageSize));
        return JSON.toJSONString (tp);
    }
    @RequestMapping("/languageSelect")
    public String  languageSelect(@RequestParam int pageNumber, @RequestParam int pageSize) {
        TablePage tp= pageUtil.getDataForPaging(serverService.getLanguageType(pageNumber, pageSize));
        return JSON.toJSONString (tp);
    }
    @RequestMapping("/deploySelect")
    public String  deploySelect(@RequestParam int pageNumber, @RequestParam int pageSize) {
        TablePage tp= pageUtil.getDataForPaging(serverService.getDeployType(pageNumber, pageSize));
        return JSON.toJSONString (tp);
    }
    @RequestMapping("/systemSelect")
    public String  SystemSelect(@RequestParam int pageNumber, @RequestParam int pageSize) {
        TablePage tp= pageUtil.getDataForPaging(serverService.getSystemType(pageNumber, pageSize));
        return JSON.toJSONString (tp);
    }

    @RequestMapping(method = RequestMethod.POST,value = "/addBaseServerSC" )
    public String addBaseServerCt(@RequestBody BaseServerEntity baseServerEntity){
        int result = serverService.AddBaseServerSC(baseServerEntity);
        if (result > 0){
            return  JSON.toJSONString("ture");
        }else {
            return JSON.toJSONString("false");
        }
    }
}
